/**
 * 点击列表中的某个元素，清除其他项的pro属性的值，并将点击的项的pro属性值赋值为proval
 * 
 * @param id
 */
function clickChangeBg(id, pro, proval) {
	var doc_a = $(id);
	for (var i = 0; i < doc_a.length; i++) {
		doc_a.eq(i).click(function() {
			for (var i = 0; i < doc_a.length; i++) {

				doc_a.eq(i).attr(pro, "");
			}
			doc_a.eq($(id).index(this)).attr(pro, proval);
		});
	}
}

function createPage(id, page, current) {
	var doc_page = $(id);
	doc_page.empty();
	for (var i = 0; i < page; i++) {
		var cur = i + 1;
		if (i != (current - 1)) {
			doc_page.append("<li><a role='第 " + cur + " 页' href='#'>第 " + cur
					+ " 页</a></li>");
		}
	}
}

function createPN(pre_id, next_id, page, current) {
	var doc_pre = $(pre_id);
	var doc_next = $(next_id);
	if (current == 1) {
		doc_pre.attr("class", "disabled");
	} else if (current == page) {
		doc_next.attr("class", "disabled");
	} else {
		doc_pre.attr("class", "");
		doc_next.attr("class", "");
		doc_pre.attr("role", current - 1);
		doc_next.attr("role", current + 1);
	}
}

function updateAdminStatus(adminId, status) {
	$.ajax({
		url : webRoot + "web/admin/updateStatus",
		type : "get",
		dataType : "json",
		data : {
			"adminId" : adminId,
			"status" : status
		},
		success : function(data) {
			if (data.result == "success") {
				alert("操作成功！");
				location.reload();
			} else {
				alert("操作失败！");
			}
		}
	});
}

function updateBuStatus(buId, status) {
	$.ajax({
		url : webRoot + "web/building/updateStatus",
		type : "get",
		dataType : "json",
		data : {
			"buId" : buId,
			"status" : status
		},
		success : function(data) {
			if (data.result = "success") {
				alert("操作成功！");
				location.reload();
			} else {
				alert("操作失败！");
			}
		}
	});
}

function delBu(buId) {
	if (confirm('确实要删除该内容吗?')) {
		$.ajax({
			url : webRoot + "web/building/delete",
			type : "get",
			dataType : "json",
			data : {
				"buId" : buId
			},
			success : function(data) {
				if (data.result == "success") {
					alert("操作成功！")
					location.reload();
				} else {
					alert("操作失败！");
				}
			}
		});
	}
}

function updateCrStatus(crId, status) {
	$.ajax({
		url : webRoot + "web/classroom/update",
		type : "get",
		dataType : "json",
		data : {
			"crId" : crId,
			"status" : status
		},
		success : function(data) {
			if (data.result == "success") {
				alert("操作成功！");
				location.reload();
			} else {
				alert("操作失败！");
			}
		}
	});
}

function delCr(crId, buId) {
	if (confirm('确实要删除该内容吗?')) {
		$.ajax({
			url : webRoot + "web/classroom/del",
			type : "get",
			dataType : "json",
			data : {
				"crId" : crId,
				"buId" : buId
			},
			success : function(data) {
				if (data.result == "success") {
					alert("操作成功！")
					location.reload();
				} else {
					alert("操作失败！");
				}
			}
		});
	}
}

function choDesk(length, width, crId) {
	$("#crId").val(crId);
	var room = $("#room");
	room.empty();
	var desks = null;
	$.ajax({
		url : webRoot + "web/desk/query",
		type : "get",
		async : false,
		dataType : "json",
		data : {
			"crId" : crId,
			"length" : length
		},
		success : function(data) {
			if (data.result == "success") {
				desks = data.deskNo;
			}
		}
	});
	for (i = 0; i < width; i++) {
		room.append("<p></p>");
		var p = room.find("p:last");
		for (j = 0; j < length; j++) {
			var num = i * length + j + 1;
			p.append("<a href='#' class='seat active' role='" + num + "'></a>");
		}
	}
	if (desks != null) {
		desks = desks.substring(1, desks.length - 1);
		var ds = desks.split(",");
		var as = room.find("a");
		for ( var i in ds) {
			as.eq(ds[i] - 1).attr("class", "seat active selected");
		}
	}
	$("#room a").click(function() {
		var val = $(this).attr("class");
		if (val == "seat active") {
			$(this).attr("class", "seat active selected");
		} else {
			$(this).attr("class", "seat active");
		}
	});
	$("#desk").modal("show");
}

function updateStuStatus(stuId, status) {
	$.ajax({
		url : webRoot + "web/stu/update",
		type : "get",
		dataType : "json",
		data : {
			"stuId" : stuId,
			"status" : status
		},
		success : function(data) {
			if (data.result == "success") {
				alert("修改成功！");
				location.reload();
			}
		}
	});
}
function reset(stuId) {
	$.ajax({
		url : webRoot + "web/stu/reset",
		type : "get",
		dataType : "json",
		data : {
			"stuId" : stuId
		},
		success : function(data) {
			if (data.result == "success") {
				alert("重置成功，密码为该学生的学号！");
			}
		}
	});
}

function stuDetail(stuNo) {
	var info = null;
	$.ajax({
		url : webRoot + "web/stu/info",
		type : "get",
		async : false,
		dataType : "json",
		data : {
			"stuNo" : stuNo
		},
		success : function(data) {
			if (data != null) {
				info = data;
			}
		}
	});
	if (info == null) {
		alert("暂没有详细信息！");
	} else {
		$("#rec").empty().append(info.recNum);
		$("#sure").empty().append(info.sureNum);
		$("#cancel").empty().append(info.cancelNum);
		$("#info").modal("show");
	}

}
function getRec(buId) {
	if (buId > 0) {
		var rec = null;
		$("#loading").modal("show");
		$.ajax({
			url : webRoot + "client/rec/getRec",
			type : "get",
			async : false,
			dataType : "json",
			data : {
				"buId" : buId
			},
			success : function(data) {
				if (data != null) {
					rec = data;
				}
			}
		});
		$("#loading").modal("hide");
		if (rec != null)
			showRec(rec);
		else
			alert("预约失败，可能是次数限制！");
	} else {
		alert("请选择教学楼后再随机预约！");
	}
}

function record(buId, crId, crNo, length, width) {
	var desk = null;
	$.ajax({
		url : webRoot + "client/desk/query",
		type : "get",
		async : false,
		dataType : "json",
		data : {
			"crId" : crId
		},
		success : function(data) {
			if (data != null) {
				desk = data;
			}
		}
	});
	if (desk != null) {
		var room = $("#room");
		room.empty();
		for (var i = 0; i < width; i++) {
			room.append("<p></p>");
			var p = room.find("p:last");
			for (var j = 0; j < length; j++) {
				var num = i * length + j + 1;
				p.append("<a class='seat' title='" + num + "'></a>");
			}
		}
		var as = room.find("a");
		// 渲染座位
		$.each(desk, function(index, value) {
			var a = as.eq(value.deskNo - 1);
			if (value.deskStatus == 2) {
				a.attr("class", "seat disabled");
			} else {
				a.attr("class", "seat active");
				a.attr("role", value.deskId);
				a.attr("href", "#");
			}
		});
		$("#desk").modal("show");
		var deskId = null;
		var deskNo = null;
		var isCheck = false;
		$("#room a[class='seat active']").click(function() {

			// 这里的选择还有问题。
			if (isCheck == false) {
				$(this).attr("class", "seat active selected");
				deskId = $(this).attr("role");
				deskNo = $(this).attr("title");
				isCheck = true;

			} else {
				var val = $(this).attr("class");
				if (val == "seat active selected") {
					$(this).attr("class", "seat active");
					deskId = null;
					deskNo = null;
					isCheck = false;
				} else {
					alert("您已经选择座位！");
				}
			}
		});
		var rec = null;
		$("#sure").unbind("click");
		$("#sure").click(function() {
			if (isCheck == true) {
				$.ajax({
					url : webRoot + "client/rec/add",
					type : "post",
					async : false,
					dataType : "json",
					data : {
						"buId" : buId,
						"crId" : crId,
						"crNo" : crNo,
						"deskId" : deskId,
						"deskNo" : deskNo
					},
					success : function(data) {
						if (data != null) {
							rec = data;
						}
					}
				});

				if (rec != null) {
					// 将成功的预约记录显示
					showRec(rec);
					$("#desk").modal("hide");
				} else {
					alert("预约失败，可能是次数限制！");
				}

			} else {
				alert("您还没有选择座位！");
			}
		});
	} else {
		alert("没有可预约的座位");
	}
}

function showRec(rec) {
	$("#bu").empty().append(rec.buName);
	$("#rm").empty().append(rec.crNo);
	$("#ds").empty().append(rec.deskNo);
	$("#date").empty().append(rec.recDate);
	$("#record").modal("show");
}

function sureID(url) {
	$("#ID_card").modal("show");
	$("#sure").unbind("click");
	$("#sure").click(function(){
		var stuNo = $("[name='stuNo']").val();
		var stuPass = $("[name='stuPass']").val();
		if(stuNo != "" && stuPass != ""){
			$.ajax({
				url:webRoot+url,
				type:"post",
				dataType:"json",
				data:{
					"stuNo":stuNo,
					"stuPass":stuPass
				},
				success:function(data){
					if(data.result == "sure_success"){
						alert("确认成功！");
						$("#ID_card").modal("hide");
						showRec(data.rec);
					}else if(data.result == "leave_success"){
						alert("暂时离开确认成功！");
					}else if(data.result == "cancel_success"){
						alert("预约取消成功！");
					}else if(data.result == "sure_fail"){
						alert("您已经确认！");
					}else if(data.result == "leave_fail"){
						alert("您已经离开确认成功！");
					}else if(data.result == "complete_success"){
						alert("您的座位释放成功！");
					}else if(data.result == "complete_fail"){
						alert("您没有预约的座位！");
					}else if(data.result == "no_rec"){ // 如果没有预约那么在后台会自动登陆该账户
						if (confirm("您没有预约，需要立即前往预约吗？")){
							window.location.href = webRoot + "client/classroom/query?page=1&status=1&buId="+machineBu;
						}else {
							window.location.href = webRoot + "machine/client/logout";
						}
					}else if(data.result = "cancel_fail"){
						alert("取消失败，您没有预约！");
					}else {
						alert("操作失败，参数非法！");
					}
					$("#ID_card").modal("hide");
				},
				error:function(data){
					alert(data.result);
					alert("未知错误！");
					$("#ID_card").modal("hide");
				}
			});
		}
	});
}